package com.greateamd.timetableasy.client.widgets.studyperiod;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.greateamd.timetableasy.client.services.course.CourseService;
import com.greateamd.timetableasy.client.services.course.CourseServiceAsync;
import com.greateamd.timetableasy.client.widgets.EntityContainer;
import com.greateamd.timetableasy.client.widgets.course.CoursePanel;
import com.greateamd.timetableasy.client.widgets.cursus.CursusListBox;
import com.greateamd.timetableasy.client.widgets.event.EventPanel;
import com.greateamd.timetableasy.domain.Course;
import com.greateamd.timetableasy.domain.Event;
import com.greateamd.timetableasy.domain.StudyPeriod;

public class StudyPeriodImplementer extends Composite {

	private final CourseServiceAsync courseService = GWT.create(CourseService.class);
	
	@UiTemplate("StudyPeriodImplementer.ui.xml")
	interface ListRoomBuilderUIBinder extends UiBinder<Widget, StudyPeriodImplementer> {}
	private static ListRoomBuilderUIBinder uiBinder = GWT.create(ListRoomBuilderUIBinder.class);
	
	@UiField CursusListBox listOfCursusAvailable;
	@UiField EntityContainer<StudyPeriod> listOfSp;
	@UiField EntityContainer<StudyPeriod> listOfSpChoosen;
	@UiField EntityContainer<Course> listOfCoursesImpl;
	@UiField EntityContainer<Event> listOfEventsImpl;
	@UiField Button addSpBt;
	@UiField Button rmSpBt;
	@UiField CoursePanel coursePanel;
	@UiField EventPanel eventPanel;
	
	public StudyPeriodImplementer() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	//---------------------------------------------------------------------
	//-------------------------- Handler
	@UiHandler("listOfCursusAvailable")
	void onCursusChange(ChangeEvent e){
		if(listOfCursusAvailable.getSelectedCursus() != null){
			listOfSp.clear();
			for(StudyPeriod sp : listOfCursusAvailable.getSelectedCursus().getStudyPeriods()){
				listOfSp.addEntity(sp);
			}
		}
	}
	
	@UiHandler("listOfSpChoosen")
	void onSpChoosenChange(ChangeEvent e){
		if(listOfSpChoosen.getSelectedEntity() != null){
			loadCourseList(listOfSpChoosen.getSelectedEntity());
		}
	}

	@UiHandler("listOfCoursesImpl")
	void onCourseChange(ChangeEvent e){
		if(listOfCoursesImpl.getSelectedEntity() != null){
			coursePanel.setCourse(listOfCoursesImpl.getSelectedEntity());
			listOfEventsImpl.clear();
			for(Event event : listOfCoursesImpl.getSelectedEntity().getEvents()){
				listOfEventsImpl.addEntity(event);
			}
		}
	}
	
	@UiHandler("listOfEventsImpl")
	void onEventChange(ChangeEvent e){
		if(listOfEventsImpl.getSelectedEntity() != null){
			eventPanel.setEvent(listOfEventsImpl.getSelectedEntity());
		}
	}

	@UiHandler("addSpBt")
	void onAddSpBtClick(ClickEvent e){
		if(listOfSp.getSelectedEntity() != null){
			listOfSpChoosen.addEntity(dupicateStudy(listOfSp.getSelectedEntity()));
		} else {
			Window.alert("You must select a Study period");
		}
	}

	@UiHandler("rmSpBt")
	void onRemoveSpBtClick(ClickEvent e){
		listOfSpChoosen.removeSelectedEntity();
	}

	//---------------------------------------------------------------------
	//-------------------------- UI action
	private void setCourseList(Collection<Course> list){
		listOfCoursesImpl.clear();
		for(Course course : list){
			course.setId(null);
			listOfCoursesImpl.addEntity(course);
//			listOfCoursesImpl.addEntity(dupicateCourse(course));
		}
	}
	
	//---------------------------------------------------------------------
	//-------------------------- Server call
	
	private StudyPeriod dupicateStudy(StudyPeriod sp) {
		StudyPeriod newSp = new StudyPeriod(sp.getName(),sp.getType(), sp.getCursus());
		newSp.setId(sp.getId());
		Set<Course> courses = new HashSet<Course>();
		for(Course c : sp.getCourses()){
			Course newCourse = new Course(c.getName(),c.getType(),c.getStartDate(),c.getEndDate(),null);
			Set<Event> events = new HashSet<Event>();
			for(Event e : c.getEvents()){
				events.add(new Event(e.isMandatory(),e.getLabel(),e.getLocation(),e.getEventType(),e.getEventModality(),e.getStartDate(),e.getDuration(),e.getDispenser(),e.isEnabled()));
			}
			newCourse.setEvents(events);
			courses.add(newCourse);
		}
		newSp.setCourses(courses);
		return newSp;
	}

	private void loadCourseList(final StudyPeriod sp) {
		setCourseList(sp.getCourses());
	}
	
	public EntityContainer<StudyPeriod> getListOfSpChoosen() {
		return listOfSpChoosen;
	}

	public List<Course> getCourses() {
		return listOfCoursesImpl.getEntities();
	}
}


//if(sp.getCourses() == null || sp.getCourses().size()==0){
//new ServerCall<List<Course>>() {
//
//	@Override
//	protected void callService(AsyncCallback<List<Course>> asyncCallback) {
//		StudyPeriod newSp = new StudyPeriod(sp.getName(), null,true);
//		newSp.setId(sp.getId());
//		courseService.getAllOfStudyPeriod(newSp,asyncCallback);
//	}
//
//	@Override
//	public void onSuccess(List<Course> result) {
//		setCourseList(result);
//	}
//}.call();
//} else {
//	setCourseList(sp.getCourses());
//}